<?php

namespace iBrand\Shop\Core\Models;

use Illuminate\Database\Eloquent\Model;
use DB;

class Shop extends Model
{
    protected $guarded = ['id'];

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $prefix = config('ibrand.app.database.prefix', 'ibrand_');

        $this->setTable($prefix . 'o2o_shop');
    }

    public function city()
    {
        return $this->hasOne(City::class, 'id', 'city');
    }

    public static function getStoreNameByIds($ids)
    {
        $string = '';
        if (!$ids) {
            return $string;
        }

        $model = new self();
        $shops = $model->whereIn('id', explode(',', $ids))->get(['name'])->toArray();

        $string = implode(',', array_column($shops, 'name'));

        return $string;
    }

    public function brands()
    {
        return $this->belongsToMany(Brand::class, 'o2o_brand', 'shop_id', 'brand_id')->withPivot('discount');
    }
}

